<?php
class Business_line_dao extends Base_dao
{	
	function __construct()
    {
		parent::__construct();
		
		$this->nombreTabla = 'businessline';
		$this->nombreColumnaID = 'id';
		$this->nombreModelo = 'Business_line_dto';
		
		$this->load->model("$this->nombreModelo");
	}
	
	public function guardar($objBLineDTO)
	{
		$result = parent::guardar($this->nombreTabla, $this->nombreColumnaID, $objBLineDTO, $objBLineDTO->id);
		
		if($result['exito'])
		{
			if ($objBLineDTO->id == 0) { $objBLineDTO->id = $result['idGenerado']; }
		}
		
		return $result;
	}

	
	public function borrar($idBLine)
	{
		$result = parent::borrar($this->nombreTabla, $this->nombreColumnaID, $idBLine);	
		return $result;
	}
	
	public function obtenerDatos($condiciones = array(), $crearObjetos = false, $limite = 0)
	{	
		return parent::obtenerDatos($this->nombreTabla, $this->nombreModelo, $condiciones, $crearObjetos, $limite);
	}
	
	public function obtenerObjetos($condiciones = array(), $limite = 0)
	{
		return $this->obtenerDatos($condiciones, true, $limite);
	}
	
	public function obtenerObjetoPorID($idBuscado)
	{
		$obj = parent::obtenerObjetoPorID($this->nombreTabla, $this->nombreColumnaID, $this->nombreModelo, $idBuscado);
		if($obj == NULL) $obj = new Business_line_dto();
		
		return $obj;
	}
		
	public function getTreeStructure($idBusinessLine = 0)
	{
		// Obtiene la informacion de todas las entradas correspondientes a una ronda
		$this->db->select("id, name");
		
		if($idBusinessLine != 0) $this->db->where("$this->nombreTabla.id", "$idBusinessLine");		
		$this->db->order_by("$this->nombreTabla.id", 'asc');
		
		return $this->db->get("$this->nombreTabla");
	}

	public function getBLWithInfo($idRound)
	{
		// Obtiene la informacion de todas las entradas correspondientes a una ronda
		$tableName_inputs = 'roundinput';
		$tableName_indicator = 'indicator';
		$tableName_output = 'output';
		$tableName_outcome = 'outcome';
		$tableName_businessline = 'businessline';
		
		$this->db->select("$tableName_businessline.*");
		
		$this->db->join("$tableName_outcome", "$tableName_outcome.idBusinessLine = $tableName_businessline.id");
		$this->db->join("$tableName_output", "$tableName_output.idOutcome = $tableName_outcome.id");
		$this->db->join("$tableName_indicator", "$tableName_indicator.idOutput = $tableName_output.id");
		$this->db->join("$tableName_inputs", "$tableName_inputs.idIndicator = $tableName_indicator.id");
		
		$this->db->where("$tableName_inputs.idRound", "$idRound");

		$this->db->group_by("$tableName_businessline.id");
		
		$consulta = $this->db->get("$tableName_businessline");
		$arreglo = array();
		if ($consulta->num_rows() > 0)
		{
			$indice = 0;
			foreach ($consulta->result("$this->nombreModelo") as $row)
			{
				$arreglo[$indice] = $row;
				$indice++;
			}
		}

		return $arreglo;
	}
}